//
//  BZUCLoginVContainerV.m
//  Pods
//
//  Created by quheng on 2017/8/29.
//
//

#import "BZUCLoginWrapperV.h"
#import <Masonry/Masonry.h>
#import "BZUCInputVModel.h"
#import <BZPreHeaderComponent/BZPreHeaderComponent.h>



#define keyboardSperatorH 10

@interface BZUCLoginWrapperV ()<BZUCLoginVDelegate>




@end

@implementation BZUCLoginWrapperV


- (instancetype)init{

    self = [super init];
    
    if (self) {
        
        [self configureV];
        
    }

    return self;
}

- (void)configureV{

    //注册键盘出现的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil];

    
    BZUCLoginV *loginV = [[BZUCLoginV alloc]init];
    [self addSubview:loginV];
    
    loginV.delegate = self;
    
    self.loginV = loginV;
    
    [loginV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.size.equalTo(self);
    }];
}


- (void)setViewData:(NSArray *)viewData{

    _viewData = viewData;
    
    for (BZUCInputVModel *model in viewData) {
        
        NSUInteger i = [viewData indexOfObject:model];
        
        if (i == 0) {
            self.loginV.accountV.inputVModel = viewData[i];
        }else{
            self.loginV.pswV.inputVModel = viewData[i];
        }
    }
}


///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    
        CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
        self.contentSize = CGSizeMake(0,self.self.frame.size.height +kbHeight);
    
        CGFloat offset = CGRectGetMaxY(self.loginV.loginBtn.frame)+keyboardSperatorH - (self.loginV.frame.size.height - kbHeight);
    
    if (offset > 0) {
        
        self.contentOffset = CGPointMake(0, offset);
    }else{
    
        self.contentOffset = CGPointMake(0, 0);
    
    }
    
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {

        self.contentOffset = CGPointMake(0,0);
        self.contentSize = CGSizeMake(SCREEN_WIDTH,SCREEN_HEIGHT);
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}


- (void)didLoginSuccess:(BZUCLoginV *)loginV{

    if (self.delegate && [self.delegate respondsToSelector:@selector(loginSuccess:)]) {
        
        [self.delegate loginSuccess:self];
    }
}

@end
